/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-22 16:39
 **/

package com.second.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.second.common.pojo.R;
import com.second.common.util.JwtUtil;
import com.second.common.util.Payload;
import com.second.system.config.RsaKeyProperties;
import com.second.system.dao.MenuDao;
import com.second.system.pojo.entity.Menu;
import com.second.system.pojo.entity.User;
import com.second.system.pojo.utils.MyAnnotation;
import com.second.system.pojo.utils.WebUtil;
import com.second.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 0)
    public R showUserHasMenu() {

        // 获取token
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("Token");
        // 解析 token
        Payload<User> infoFromToken = JwtUtil.getInfoFromToken(token, rsaKeyProperties.getPublicKey(), User.class);
        // 获取用户信息
        User userInfo = infoFromToken.getUserInfo();
        List<Menu> menuList = menuDao.showUserHasMenu(userInfo.getUserid());
        if (menuList.size()>0) {
           return R.success().putListData(menuList);
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 0)
    public R showAllMenu() {

        List<Menu> menuList = menuDao.showAllMenu();
        return R.success().putListData(menuList);
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 0)
    public R showRoleHasMenuId(Integer roleid) {

        List<Integer> menuidList = menuDao.showRoleHasMenuId(roleid);
        return R.success().putListData(menuidList);
    }

    @Transactional
    @Override
    @MyAnnotation( title = "菜单模块",businessType = 2)
    public R updateRoleHasMenu(List<Integer> menuidList, Integer roleid) {

        // 先删除之前的
        menuDao.deleteOldMenu(roleid);
        // 再新增最新的
        menuDao.insertNewMenu(menuidList,roleid);
        return R.success();
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 0)
    public R showAllFatherMenu() {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mpid",0);
        List menuList = menuDao.selectList(queryWrapper);
        return R.success().putListData(menuList);
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 1)
    public R addMenu(Menu menu) {

        int i = menuDao.insert(menu);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 0)
    public R showMenu() {

        List<Menu> menuList = menuDao.selectList(null);
        return R.success().putListData(menuList);
    }

    @Override
    @MyAnnotation( title = "菜单模块",businessType = 3)
    public R deleteMenuByMenuid(Integer mid) {

        int i = menuDao.deleteById(mid);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }
}
